/**
 * 日期插件格式化方法
 * @param type
 * @param val
 * @returns {string|*}
 */
export const formatter = (type, val) => {
  if (type === 'year') {
    return `${val}年`
  } else if (type === 'month') {
    return `${val}月`
  } else if (type === 'day') {
    return `${val}日`
  } else if (type === 'hour') {
    return `${val}时`
  } else if (type === 'minute') {
    return `${val}分`
  }
  return val
}

/**
 * 将格式为HH:mm:ss 的时间新增一秒
 * 格式必须为HH:mm:ss 如 00:00:01
 */
export const timeAddOneSecond = (val) => {
  // 获取秒数
  let allSecond = HHmmssToSecond(val)
  // 秒数加1
  allSecond += 1
  // 将秒数转为时间格式
  return formateSecondToTime(allSecond)
}

// 把秒转换成HH:mm:ss
  export function dealTime(seconds) {
    let time = 0
    if (seconds > -1) {
      let hour = Math.floor(seconds / 3600)
      let min = Math.floor(seconds / 60) % 60
      let sec = seconds % 60
      if (hour < 10) {
        time = '0' + hour + ':'
      } else {
        time = hour + ':'
      }

      if (min < 10) {
        time += '0'
      }
      time += min + ':'
      if (sec < 10) {
        time += '0'
      }
      time += sec.toFixed(0)
    }
    return {
      time,
      seconds,
    }
  }

/**
 * 将HH:mm:ss 转为秒
 */
const HHmmssToSecond = (val) => {
  // 拆分时间
  const splits = val.split(':')
  // 获得秒数
  const secondHour = Number(splits[0]) * 60 * 60
  const secondMin = Number(splits[1]) * 60
  return secondHour + secondMin + Number(splits[2])
}

/**
 * 将秒转为时分秒
 * @param time
 * @returns {string}
 */
function formateSecondToTime(time) {
  const h = parseInt(time / 3600)
  const minute = parseInt(time / 60 % 60)
  const second = Math.ceil(time % 60)

  const hours = h < 10 ? '0' + h : h
  const formatSecond = second > 59 ? 59 : second
  return `${hours > 0 ? `${hours}:` : '00:'}${minute < 10 ? '0' + minute : minute}:${formatSecond < 10 ? '0' + formatSecond : formatSecond}`
}

